package com.ledim.db;

import android.text.TextUtils;
import com.ledim.bean.ChatRoomBean;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.bean.LedimUserBean;
import com.ledim.db.dao.ChatRoomBeanDao;
import com.ledim.db.dao.LedimChoiceCardBeanDao;
import com.ledim.utils.ConstantUtil;
import java.util.Collection;
import java.util.List;
import org.greenrobot.greendao.AbstractDao;

/**
 * Created by ccx on 16/11/3. 数据库业务逻辑工具类
 */

public class LedimDBUtils
{

	public static void clearAllData()
	{
		Collection<AbstractDao<?, ?>> daos = LedimDBManager.getInstance()
		        .getDaoSession().getAllDaos();
		for (AbstractDao<?, ?> dao : daos)
		{
			dao.deleteAll();
		}
	}

	/**
	 * @param cardBean
	 *            存储卡片数据
	 */
	public static void saveChoiceCard(final LedimChoiceCardBean cardBean)
	{

		LedimDBManager.getInstance().getDaoSession().runInTx(new Runnable()
		{
			@Override
			public void run()
			{

				if (cardBean.getId() == null || cardBean.getId() == 0)
				{
					if (cardBean.photo != null)
					{
						long photoId = LedimDBManager.getInstance()
						        .getDaoSession().getLedimPicBeanDao()
						        .insert(cardBean.photo);
						if (photoId == 0)
						{
							return;
						}
						cardBean.setPhotoId(photoId);
					}
					LedimDBManager.getInstance().getDaoSession()
					        .getLedimChoiceCardBeanDao().insert(cardBean);

				}
				else
				{
					if (null != cardBean.photo)
					{
						LedimDBManager.getInstance().getDaoSession()
						        .getLedimPicBeanDao().update(cardBean.photo);
					}

					LedimDBManager.getInstance().getDaoSession()
					        .getLedimChoiceCardBeanDao().update(cardBean);
				}
			}
		});

	}

	/**
	 * @param cardBean
	 *            删除卡片
	 */
	public static void deleteChoiceCard(final LedimChoiceCardBean cardBean)
	{

		LedimDBManager.getInstance().getDaoSession().getLedimChoiceCardBeanDao()
		        .delete(cardBean);
		if (cardBean.photo != null)
		{
			LedimDBManager.getInstance().getDaoSession().getLedimPicBeanDao()
			        .delete(cardBean.photo);
		}
	}

	/**
	 * @param cards
	 *            删除所有卡片
	 */
	public static void deleteAllChoiceCards(
	        final List<LedimChoiceCardBean> cards)
	{
		if (cards != null && cards.size() > 0)
		{
			LedimDBManager.getInstance().getDaoSession().runInTx(new Runnable()
			{

				@Override
				public void run()
				{
					for (LedimChoiceCardBean card : cards)
					{
						deleteChoiceCard(card);
					}
				}
			});
		}
	}

	/**
	 * @param cardBean
	 * @return 查询card 并关联查询图片
	 */
	public static LedimChoiceCardBean queryChoiceCard(
	        LedimChoiceCardBean cardBean)
	{
		LedimChoiceCardBean dbCardBean = null;
		List<LedimChoiceCardBean> cards = null;
		if (cardBean.type == ConstantUtil.CardType.video)
		{
			if (!TextUtils.isEmpty(cardBean.submedia))
			{
				cards = LedimDBManager.getInstance().getDaoSession()
				        .getLedimChoiceCardBeanDao().queryBuilder()
				        .where(LedimChoiceCardBeanDao.Properties.Submedia
				                .eq(cardBean.submedia))
				        .list();
			}
		}
		else
		{
			String media = cardBean.media;
			if (TextUtils.isEmpty(media))
			{
				if (!TextUtils.isEmpty(cardBean.link))
				{
					String link = cardBean.link.replace("app://medias/", "");
					String[] s = link.split("/");
					if (s.length > 1)
					{
						media = s[0];
						cardBean.media = media;
						cardBean.submedia = s[1];
					}
				}
			}

			if (!TextUtils.isEmpty(media))
			{
				cards = LedimDBManager.getInstance().getDaoSession()
				        .getLedimChoiceCardBeanDao().queryBuilder()
				        .where(LedimChoiceCardBeanDao.Properties.Media
				                .eq(media))
				        .list();
			}
		}
		if (cards != null && cards.size() > 0)
		{
			dbCardBean = cards.get(0);
			dbCardBean.photo = LedimDBManager.getInstance().getDaoSession()
			        .getLedimPicBeanDao().load(dbCardBean.getPhotoId());
		}
		if (dbCardBean != null)
		{
			dbCardBean.link = "app://medias/" + dbCardBean.media + "/"
			        + dbCardBean.submedia;
		}
		return dbCardBean;
	}

	/**
	 * @return 查询所有看过的Card记录
	 */
	public static List<LedimChoiceCardBean> queryChoiceCards()
	{
		List<LedimChoiceCardBean> cardLists = LedimDBManager.getInstance()
		        .getDaoSession().getLedimChoiceCardBeanDao().queryBuilder()
		        .orderDesc(LedimChoiceCardBeanDao.Properties.Time).list();

		if (cardLists != null && cardLists.size() > 0)
		{
			for (LedimChoiceCardBean cardBean : cardLists)
			{
				cardBean.photo = LedimDBManager.getInstance().getDaoSession()
				        .getLedimPicBeanDao().load(cardBean.getPhotoId());
			}
		}
		return cardLists;
	}

	/**
	 * @param roomBean
	 *            存储聊天室数据
	 */
	public static void saveChatRoom(final ChatRoomBean roomBean)
	{
		final List<ChatRoomBean> roomLists = LedimDBManager.getInstance()
		        .getDaoSession().getChatRoomBeanDao().queryBuilder()
		        .where(ChatRoomBeanDao.Properties.RoomId.eq(roomBean.roomId))
		        .list();

		LedimDBManager.getInstance().getDaoSession().runInTx(new Runnable()
		{
			@Override
			public void run()
			{
				if (roomLists == null || roomLists.size() == 0)
				{
					long coverId = LedimDBManager.getInstance().getDaoSession()
					        .getLedimPicBeanDao().insert(roomBean.cover);
					roomBean.setCoverId(coverId);
					long userAvaId = LedimDBManager.getInstance()
					        .getDaoSession().getLedimPicBeanDao()
					        .insert(roomBean.user.avatar);
					roomBean.user.setAvatarId(userAvaId);
					long userId = LedimDBManager.getInstance().getDaoSession()
					        .getLedimUserBeanDao().insert(roomBean.user);
					roomBean.setUserId(userId);

					LedimDBManager.getInstance().getDaoSession()
					        .getChatRoomBeanDao().insert(roomBean);

				}
				else
				{
					ChatRoomBean roomDBbean = roomLists.get(0);
					LedimUserBean userDBBean = LedimDBManager.getInstance()
					        .getDaoSession().getLedimUserBeanDao()
					        .load(roomDBbean.getUserId());
					roomBean.user.setId(roomDBbean.getUserId());
					roomBean.user.avatar.setId(userDBBean.getAvatarId());
					roomBean.setUserId(roomDBbean.getUserId());
					roomBean.setId(roomDBbean.getId());
					roomBean.setCoverId(roomDBbean.getCoverId());
					roomBean.cover.setId(roomDBbean.getCoverId());
					LedimDBManager.getInstance().getDaoSession()
					        .getLedimPicBeanDao().update(roomBean.cover);
					LedimDBManager.getInstance().getDaoSession()
					        .getChatRoomBeanDao().update(roomBean);
					LedimDBManager.getInstance().getDaoSession()
					        .getLedimUserBeanDao().update(roomBean.user);
					LedimDBManager.getInstance().getDaoSession()
					        .getLedimPicBeanDao().update(roomBean.user.avatar);

				}
			}
		});

	}

	/**
	 * @param roomBean
	 *            删除聊天室数据
	 */
	public static void deleteChatRoom(final ChatRoomBean roomBean)
	{
		LedimDBManager.getInstance().getDaoSession().runInTx(new Runnable()
		{
			@Override
			public void run()
			{
				LedimDBManager.getInstance().getDaoSession()
				        .getChatRoomBeanDao().delete(roomBean);
				if (roomBean.cover != null)
					LedimDBManager.getInstance().getDaoSession()
					        .getLedimPicBeanDao().delete(roomBean.cover);
				if (roomBean.user != null)
					LedimDBManager.getInstance().getDaoSession()
					        .getLedimUserBeanDao().delete(roomBean.user);
				if (roomBean.user.avatar != null)
					LedimDBManager.getInstance().getDaoSession()
					        .getLedimPicBeanDao().delete(roomBean.user.avatar);

			}
		});

	}

	/**
	 * @param rooms
	 *            删除所有聊天室
	 */
	public static void deleteAllChatRoom(final List<ChatRoomBean> rooms)
	{
		if (rooms != null && rooms.size() > 0)
		{
			LedimDBManager.getInstance().getDaoSession().runInTx(new Runnable()
			{
				@Override
				public void run()
				{
					for (ChatRoomBean room : rooms)
					{
						deleteChatRoom(room);
					}
				}
			});

		}
	}

	/**
	 * @return 查询所有看过的Room记录
	 */
	public static List<ChatRoomBean> queryChatRooms()
	{
		List<ChatRoomBean> roomLists = LedimDBManager.getInstance()
		        .getDaoSession().getChatRoomBeanDao().queryBuilder()
		        .orderDesc(ChatRoomBeanDao.Properties.Time).list();

		if (roomLists != null && roomLists.size() > 0)
		{
			for (ChatRoomBean roomBean : roomLists)
			{
				roomBean.cover = LedimDBManager.getInstance().getDaoSession()
				        .getLedimPicBeanDao().load(roomBean.getCoverId());
				roomBean.user = LedimDBManager.getInstance().getDaoSession()
				        .getLedimUserBeanDao().load(roomBean.getUserId());
				roomBean.user.avatar = LedimDBManager.getInstance()
				        .getDaoSession().getLedimPicBeanDao()
				        .load(roomBean.user.getAvatarId());
			}
		}
		return roomLists;
	}
}
